package znbkListCtfd.adapter;

import android.animation.Animator;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.andview.refreshview.recyclerview.BaseRecyclerAdapter;
import com.example.lancooznbk.R;
import znbkListCtfd.entity.WrongTopicTutoringListEntity;
import com.example.lancooznbk.utils.AnimatorHelper;
import com.example.lancooznbk.view.widget.RippleView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018\2\1 0001.
 */

public class WrongTopicTutoringListAdapter extends BaseRecyclerAdapter<WrongTopicTutoringListAdapter.ViewHolder> {

    private List<WrongTopicTutoringListEntity.ParrentQuesListBean> mList;
    private Context mContext;

  /*  private String radioIds;
    private String fillIds;
    private String otherIds;*/

    private OnItemClickListener onItemClickListener;
    private OnItemHistoryClickListener onItemHistoryClickListener;
    private int mLastPosition = -1;

    public WrongTopicTutoringListAdapter(Context mContext) {
        this.mContext = mContext;
        mList = new ArrayList<>();

       /* String[] typeIdArray = mContext.getResources().getStringArray(R.array.QuesType_THREE_Arrays);
        radioIds = typeIdArray[0];
        fillIds = typeIdArray[1];
        otherIds = typeIdArray[2];*/

    }

    public  interface OnItemClickListener{
        void onItemClick(View view, List<WrongTopicTutoringListEntity.ParrentQuesListBean> list, int position);

    }

    public  interface OnItemHistoryClickListener{
        void onItemClick(View view, List<WrongTopicTutoringListEntity.ParrentQuesListBean> list, int position);

    }

    public void setOnItemHistoryClickListener(OnItemHistoryClickListener onItemHistoryClickListener){
        this.onItemHistoryClickListener = onItemHistoryClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public ViewHolder getViewHolder(View view) {
        return new ViewHolder(view,false);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType, boolean isItem) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_rv_ctfd, parent, false);
        return new ViewHolder(view,true);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position, boolean isItem) {
        holder.item.setTag(position);
        holder.tvSearchHistoryRecommend.setTag(position);

        holder.tvSearchHistoryRecommend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemHistoryClickListener.onItemClick(v,mList, (Integer) v.getTag());
            }
        });
        holder.item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onItemClick(v,mList, (Integer) v.getTag());
            }
        });

        WrongTopicTutoringListEntity.ParrentQuesListBean modelBean = mList.get(position);
        int childAnswerType = modelBean.getTKQue().getChildList().get(0).getChildAnswerType();
        if (childAnswerType == 1){//类型一
            holder.imgQuesType.setBackgroundResource(R.mipmap.bg_item_radio_wrong);
        }else{//类型二
            holder.imgQuesType.setBackgroundResource(R.mipmap.bg_item_other_wrong);
        }
        String typeName = modelBean.getTKQue().getTypeName().substring(0,
                modelBean.getTKQue().getTypeName().length() > 4 ? 4 : modelBean.getTKQue().getTypeName().length() );
        StringBuilder itemTypeBuffer = new StringBuilder(typeName);
        if (itemTypeBuffer.length() > 2) {
            itemTypeBuffer.insert(2, "\n");
        }
        holder.imgQuesType.setText(itemTypeBuffer.toString());

        holder.tvSummary.setText(Html.fromHtml(modelBean.getTKQue().getQuesBrief()));
        holder.tvAnserTimes.setText(Html.fromHtml(String.format(mContext.getString(R.string.answerTimes),
                modelBean.getReviewedCount())));
        holder.tvSearchHistoryRecommend.setTag(position);
        if (modelBean.isHistoryQuesExistFlag()){
            holder.tvSearchHistoryRecommend.setVisibility(View.VISIBLE);
        }else {
            holder.tvSearchHistoryRecommend.setVisibility(View.INVISIBLE);
        }

        String lastAnswerTimeStr = modelBean.getUpdateTime().equals(modelBean.getCreateTime()) ?
                "未作答" :modelBean.getUpdateTime();
        if(!lastAnswerTimeStr.equals("未作答")){
            lastAnswerTimeStr = lastAnswerTimeStr.substring(0,10).replaceAll("-",".");
        }
        String lastAnswerTime =  String.format(mContext.getString(R.string.paper_lastAnswerTime),lastAnswerTimeStr);
        holder.tvLastTime.setText(lastAnswerTime);

        String createAnswerTime = String.format(mContext.getString(R.string.paper_createAnswerTime),modelBean.getCreateTime().substring(0,10).replaceAll("-","."));
        holder.tvCreateTime.setText(createAnswerTime);

        if(modelBean.getFinishedStep()==0){
            holder.pbFinished.setProgress(0);
        }else if(modelBean.getFinishedStep()==1){
            holder.pbFinished.setProgress(50);
        }else if(modelBean.getFinishedStep()==2){
            holder.pbFinished.setProgress(100);
        }
//        Drawable drawable = ContextCompat.getDrawable(mContext,R.drawable.progressbar_bar_drawable_darkgreen);
//        drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());
//        holder.pbFinished.setProgressDrawable(drawable);

        int adapterPosition = holder.getAdapterPosition();

        boolean isScrollDown = adapterPosition > mLastPosition;

        for (Animator anim : AnimatorHelper.getRecyclerAnimator(holder.itemView,isScrollDown)) {
            anim.start();
        }
        mLastPosition = adapterPosition;

    }

    public void clearAndAdd(List<WrongTopicTutoringListEntity.ParrentQuesListBean> listBean) {
        mLastPosition = -1;
        mList.clear();
        mList.addAll(listBean);
        notifyDataSetChanged();
    }

    public void add(List<WrongTopicTutoringListEntity.ParrentQuesListBean> listBean){
        int startPosition = getStart() + getAdapterItemCount();
        mList.addAll(listBean);
        notifyItemRangeInserted(startPosition,listBean.size());
    }



    @Override
    public int getAdapterItemCount() {
        return mList.size();
    }



    class ViewHolder extends RecyclerView.ViewHolder{
        private RippleView rippleView;
        private TextView imgQuesType;
        private TextView tvSummary;
        private TextView tvAnserTimes;
        private TextView tvSearchHistoryRecommend;
        private TextView tvLastTime;//最近作答
        private TextView tvCreateTime;
        private ProgressBar pbFinished;//作答进度
        private RelativeLayout item;
        public ViewHolder(View itemView,boolean isItem) {
            super(itemView);
            if (isItem){
                rippleView = (RippleView) itemView.findViewById(R.id.ripple_tree_item);
                imgQuesType = (TextView) itemView.findViewById(R.id.img_quesType);
                tvSummary = (TextView) itemView.findViewById(R.id.tv_summary);
                tvAnserTimes = (TextView) itemView.findViewById(R.id.tv_answerTimes);
                tvSearchHistoryRecommend = (TextView) itemView.findViewById(R.id.tv_search_history_recommend);
                tvLastTime = (TextView) itemView.findViewById(R.id.tv_lastTime);
                tvCreateTime = (TextView) itemView.findViewById(R.id.tv_createTime);
                pbFinished = (ProgressBar) itemView.findViewById(R.id.pb_finished);
                item = (RelativeLayout) itemView.findViewById(R.id.rel_wrong_item);
            }
        }
    }
}
